package com.android.pagemarket.ui.widget;

import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.io.protocol.out.SchedulLine;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 排期视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SchedulView extends LinearLayout
{    
    private View[] rulerViews;
    private TextView[] lineNameViews;
    private LinearLayout[] lineLays;
    private List<SchedulLineView> mLineItems;
    /////////////////////////////////////////////////////////////////////////////////////////
    public SchedulView(Context context)
    {
        super(context, null);        
    }
    
    public SchedulView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        View row = inflate(getContext(), R.layout.view_schedul, null);
        addView(row,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        
        initViews();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {      
        super.onLayout(changed, l, t, r, b);
        updateViews();
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    ///////////////////////////////////////////////////////////////////////////////
    
    private void initViews()
    {
        LinearLayout ruler_lay = (LinearLayout)findViewById(R.id.ruler_lay);
        rulerViews = new View[25];
        for(int i=0; i<25; i++)
        {
            rulerViews[i] = ruler_lay.getChildAt(i);
        }
        
        lineNameViews = new TextView[4];
        lineNameViews[0] = (TextView)findViewById(R.id.line_1_name);
        lineNameViews[1] = (TextView)findViewById(R.id.line_2_name);
        lineNameViews[2] = (TextView)findViewById(R.id.line_3_name);
        lineNameViews[3] = (TextView)findViewById(R.id.line_4_name);
        
        lineLays = new LinearLayout[4];
        lineLays[0] = (LinearLayout)findViewById(R.id.line_lay_1);
        lineLays[1] = (LinearLayout)findViewById(R.id.line_lay_2);
        lineLays[2] = (LinearLayout)findViewById(R.id.line_lay_3);
        lineLays[3] = (LinearLayout)findViewById(R.id.line_lay_4);  
        
        mLineItems = new Vector<SchedulLineView>();
    }
    
    private void updateViews()
    {
        LinearLayout line = null;
        int startIndex = 0;
        int endIndex = 0;
        int titleHeight = lineNameViews[0].getBottom();
        int top = 0;
        int buttom = 0;
        int rulerHeight = rulerViews[2].getHeight();
                
        for(SchedulLineView one : mLineItems)
        {
            line = lineLays[one.getLineIndex()];
            startIndex = one.getStartIndex();
            endIndex = one.getEndIndex();
            
            top = rulerViews[startIndex].getBottom() - titleHeight  -6 + (int)((one.getStartIndexM()/60.0f)*rulerHeight);
            buttom = rulerViews[endIndex].getBottom() - titleHeight -6 +  (int)((one.getEndIndexM()/60.0f)*rulerHeight);
            
            if(one.isPreLine())
            {
                one.layout(0, 0, line.getWidth(), buttom);
            }else
            if(one.isNextLine())
            {
                one.layout(0, top, line.getWidth(), line.getHeight());
            }else
            {
                one.layout(0, top, line.getWidth() , buttom);
            }
        }
    }
    
    public void bindData(List<SchedulLine> list)
    {
        for(LinearLayout one : lineLays)
        {
            one.removeAllViews();
        }
        if(list != null)
        {            
            for(SchedulLine one : list) 
            {
                SchedulLineView preUnit = null;
                lineNameViews[one.getIndex()].setText(one.linkName);
                if(one.linkList != null)
                {
                    for(SchedulLine.LineUnit line : one.linkList)
                    {                        
                        SchedulLineView item = (SchedulLineView)inflate(getContext(), R.layout.view_schedul_item, null);
                        item.bindData(one.getIndex(), line);
                        lineLays[one.getIndex()].addView(item,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                        mLineItems.add(item);
                        if(preUnit != null)
                        {
                            item.isShowSperater(preUnit);                           
                        }
                        preUnit = item;
                    }
                }
            }
        }
        postInvalidate();
    }
}
